﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace KeywordAuction.Utility
{
    public class ExcelExporterUtility
    {
        private void exportAdspot(Microsoft.Office.Interop.Excel.Application excelApp, IEnumerable<KeywordAuction.AdspotLog> adpostLog)
        {
            excelApp.Workbooks.Add();
            var workSheet = (Microsoft.Office.Interop.Excel.Worksheet)excelApp.ActiveSheet;
            int row = 1;
            foreach (var item in adpostLog)
            {
                int column = 1;
                workSheet.Cells[row, column] = item.Name;
                column++;
                foreach (var log in item.Log)
                {
                    int topRow = row;
                    workSheet.Cells[topRow, column] = log.Revenue;
                    topRow++;
                    foreach (var logItem in log.Log)
                    {
                        workSheet.Cells[topRow, column] = logItem.Value;
                        topRow++;
                    }
                    column++;
                }
                row += item.Log.First().Log.Count +1;
            }


            workSheet.Columns[1].AutoFit();
            workSheet.Columns[2].AutoFit();
        }

        private void exportAdvertiser(Microsoft.Office.Interop.Excel.Application excelApp, IEnumerable<KeywordAuction.AdvertiserLog> advertiserLog)
        {
            excelApp.Workbooks.Add();
            var workSheet = (Microsoft.Office.Interop.Excel.Worksheet)excelApp.ActiveSheet;
            int row = 1;
            foreach (var item in advertiserLog)
            {
                int column = 1;
                workSheet.Cells[row, column] = item.Name;
                column++;
                foreach (var log in item.Log)
                {
                    int topRow = row;
                    workSheet.Cells[topRow, column] = log.Revenue;
                    
                        topRow++;
                    workSheet.Cells[topRow, column] = log.Payment;
                    
                        topRow++;
                    
                    foreach (var logItem in log.Bids)
                    {
                        workSheet.Cells[topRow, column] = logItem;
                        topRow++;
                    }
                    column++;
                }
                row += item.Log.First().Bids.Count +2;
            }

            workSheet.Columns[1].AutoFit();
            workSheet.Columns[2].AutoFit();
        }


        public void Export(IEnumerable<KeywordAuction.AdspotLog> adpostLog, IEnumerable<KeywordAuction.AdvertiserLog> advertiserLog)
        {
            var excelApp = new Microsoft.Office.Interop.Excel.Application();
            excelApp.Visible = true;

            exportAdspot(excelApp, adpostLog);
            exportAdvertiser(excelApp, advertiserLog);
        }


        public void Export(string fileName, IEnumerable<KeywordAuction.Advertiser> advertiser)
        {

        }
    }
}
